package com.neusoft.controller;

import com.alibaba.fastjson.JSON;
import com.neusoft.dto.ArticleDto;
import com.neusoft.dto.UpdateArticleDto;
import com.neusoft.entity.ArticleQueryCondition;
import com.neusoft.entity.ArticlesEntity;
import com.neusoft.mapper.ArticleMapper;
import com.neusoft.mapper.UsersMapper;
import com.neusoft.utils.SqlSessionFactoryUtils;
import com.neusoft.utils.UserUtils;
import com.neusoft.vo.Article;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("api/articles")
public class ArticleController {

    @Resource
    ArticleMapper mapper;
    @PostMapping
    public Article createArticle(@RequestBody ArticleDto articleDto){
        long userid = UserUtils.getLoginUserId();
        com.neusoft.dto.Article article = articleDto.getArticle();

        ArticlesEntity articlesEntity = new ArticlesEntity();

        articlesEntity.setTitle(article.getTitle());
        articlesEntity.setDescription(article.getDescription());
        articlesEntity.setBody(article.getBody());

        articlesEntity.setUserId(userid);
        UUID uuid = UUID.randomUUID();
        articlesEntity.setSlug(uuid.toString());
        articlesEntity.setCreatedAt(new Timestamp(new Date().getTime()));
        articlesEntity.setUpdatedAt(new Timestamp(new Date().getTime()));

        int rows = mapper.insert(articlesEntity);

        Article articleVo = mapper.getArticleById(userid, articlesEntity.getId());
        return articleVo;

    }

    @GetMapping
    public List<Article> getArticles(String tag, String author
            , String limit, String offset){
        long limitL = 20;
        if(limit != null){
            limitL = Long.valueOf(limit);
        }
        long offsetL = 0;
        if(offset != null){
            offsetL = Long.valueOf(offset) ;
        }
        Long loginUserId = UserUtils.getLoginUserId();
        if(loginUserId == null){
            loginUserId = 0L;
        }
        ArticleQueryCondition articleQueryCondition = new ArticleQueryCondition();
        articleQueryCondition.setUid(loginUserId);
        articleQueryCondition.setAuthor(author);
        articleQueryCondition.setTag(tag);
        articleQueryCondition.setLimit(limitL);
        articleQueryCondition.setOffset(offsetL);

        List<Article> allArticles = mapper.getAllArticles(articleQueryCondition);
        return allArticles;

    }

    @GetMapping("/{aid}")
    public Article getArticle(@PathVariable("aid") String aid){
        System.out.println(aid);
        return null;
    }

    @PostMapping("/update")
    public ArticlesEntity updateArticle(@RequestBody UpdateArticleDto updateArticleDto){
        ArticlesEntity articlesEntity = new ArticlesEntity();
//        articlesEntity.setId(updateArticleDto.getId());
        articlesEntity.setBody(updateArticleDto.getBody());
        articlesEntity.setTitle(updateArticleDto.getTitle());
        articlesEntity.setDescription(updateArticleDto.getDescription());
        articlesEntity.setUserId(333L);
        articlesEntity.setSlug(UUID.randomUUID().toString());
//        int i = mapper.updateById(articlesEntity);
        int i = mapper.insert(articlesEntity);
        System.out.println(articlesEntity.getId());
        return articlesEntity;
    }
}
